
/**
 * 自定义手机属性对象
 */
export class CustomAttribute {
    /** 格式 */
    format?: string;
    /** 标签 */
    label?: string;
    /** 值 */
    value?: string;
}

/** 标识 */
export class BaseSign {
    /** 索引 */
    id?: string;
    /** 标识吗 */
    handle_code?: string;
    /** 标识名称 */
    handle_name?: string;
    /** 标识分类 */
    handle_type?: string;
    /** 用户ID */
    user_id?: string;
    /** 二维码图片 */
    qr_code?: string;
    /** 创建时间 */
    create_date?: string;
    /** 修改时间 */
    modify_date?: string;
}

export enum SignType {
    Text = 'TEXT',
    Image = 'Image',
    Other = 'Other',
    email = 'EMAIL',
    url = 'URL',
    phone = 'Phone'
}

/** 标识属性 */
export class SignAttribute {
    sign_type?: SignType | string;
    sign_value?: any;
    sign_name?: string;
    sign_key?: string | undefined;
    index: number;
}

/** 解析标识 */
export class AnalysisSign {
    handle_code?: string;
    handle_name?: string;
    attribute?: SignAttribute[];
}

export class SignAttributeLabel {
    /** 
     * 标识属性
     * @description organization:'组织结构代码'
     */
    [key: string]: string;
}

export class SignAttributeIndex {
    /** 
     * 标识属性
     * @description organization:5
     */
    [key: string]: number;
}

export class SignAttributeType {
    /** 
     * 标识属性
     * @description organization:SignType.Text
     */
    [key: string]: SignType;
}

/** 自定义类目select映射 */
export let SelectCast = {
    1: SignType.Text,
    2: SignType.Text,
    3: SignType.Image,
    4: SignType.Text,
    5: SignType.Text,
    6: SignType.Text,
    7: SignType.Text,
    8: SignType.Text,
};

/**
 * 标识转解析标识
 * @param sign 标识
 * @param signLabel 标识属性名称 
 * @param signIndex 标识属性索引
 * @param signType 标识属性类型
 * @returns 解析标识
 */
export function signConvert(
    sign: BaseSign,
    signLabel?: SignAttributeLabel,
    signIndex?: SignAttributeIndex,
    signType?: SignAttributeType
): AnalysisSign {
    let analysisSign = new AnalysisSign();
    analysisSign.handle_name = sign.handle_name;
    analysisSign.handle_code = sign.handle_code;
    analysisSign.attribute = [];
    for (const key in sign) {
        // 过滤预览标识不需要的关键字
        if (key === 'id' ||
            key === 'handle_code') {
            continue;
        }
        if (key === 'custom_attribute') {
            // 处理自定义属性
            if (sign[key]) {
                for (let index = 0; index < sign[key].length; index++) {
                    let signAttribute = new SignAttribute();
                    signAttribute.index = index;
                    signAttribute.sign_key = `custom_attribute_${index}`;
                    signAttribute.sign_name = (sign[key][index] as CustomAttribute).label;
                    signAttribute.sign_type = SelectCast[(sign[key][index] as CustomAttribute).format!];
                    debugger;
                    signAttribute.sign_value = signAttribute.sign_type === SignType.Image ? (sign[key][index] as CustomAttribute).value : [(sign[key][index] as CustomAttribute).value];
                    analysisSign.attribute.push(signAttribute);
                }
            }
        } else {
            // 处理其他必填标识属性
            let signAttribute = new SignAttribute();
            signAttribute.index = signIndex![key];
            signAttribute.sign_key = key;
            signAttribute.sign_name = signLabel![key];
            signAttribute.sign_type = signType![key];
            signAttribute.sign_value = signAttribute.sign_type === SignType.Image ? sign[key] as CustomAttribute : [sign[key]];
            // if (signAttribute.sign_type === SignType.Image) {
            //     signAttribute.sign_value = sign[key] as CustomAttribute;
            // } else {
            //     signAttribute.sign_value = [sign[key]];
            // }
            analysisSign.attribute.push(signAttribute);
        }
    }
    return analysisSign;
}
/**
 * 模块排序及是否显示对象
 */
export interface ModuleSort {
    company?: { is_show?: boolean, show_sort?: number };
    product?: { is_show?: boolean, show_sort?: number };
    // single?: { is_show?: boolean, show_sort?: number };
}